home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snpd1292.zip / LSARY.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  3KB  |  111 lines

  1. /*
  2. ** LSARY - A simple directory lister using a filename array
  3. ** A public domain C demo program by Bob Stout
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <dos.h>
  11.  
  12. /* For portability, make everything look like MSC 6 */
  13.  
  14. #if defined(__TURBOC__)
  15.  #include <dir.h>
  16.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  17.  #define _dos_findnext(b) findnext(b)
  18.  #define find_t ffblk
  19.  #define _A_SUBDIR FA_DIREC
  20.  #define attrib ff_attrib
  21.  #define name ff_name
  22.  #define size ff_fsize
  23.  #define wr_time ff_ftime
  24.  #define wr_date ff_fdate
  25.  #define _dos_getdiskfree getdfree
  26.  #define diskfree_t dfree
  27.  #define avail_clusters df_avail
  28.  #define sectors_per_cluster df_sclus
  29.  #define bytes_per_sector df_bsec
  30. #else                   /* assume MSC/QC                                */
  31.  #include <errno.h>
  32. #endif
  33.  
  34. #ifdef TRUE
  35.  #undef TRUE
  36. #endif
  37. #ifdef FALSE
  38.  #undef FALSE
  39. #endif
  40. #ifdef ERROR
  41.  #undef ERROR
  42. #endif
  43.  
  44. enum LOGICAL {ERROR = -1, SUCCESS, FALSE = 0, TRUE};
  45.  
  46. #ifndef CAST
  47.  #define CAST(new_type,old_object) (*((new_type *)&(old_object)))
  48. #endif
  49.  
  50. #define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])
  51.  
  52. struct DirEntry {
  53.       char fname[FILENAME_MAX];
  54.       struct DirEntry *next;
  55. } DirRoot = {"", NULL};
  56.  
  57. /*
  58. **  Read a directory into an array
  59. */
  60.  
  61. int ReaDirArray(char *path)
  62. {
  63.       struct find_t ff;
  64.       char *p, pattern[67];
  65.       struct DirEntry *base = &DirRoot;
  66.  
  67.       strcpy(pattern, path);
  68.       if ('/' != LAST_CHAR(pattern) && '\\' != LAST_CHAR(pattern))
  69.             strcat(pattern, "\\");
  70.       strcat(pattern, "*.*");
  71.       if (SUCCESS == _dos_findfirst(pattern, 0xff, &ff)) do
  72.       {
  73.             struct DirEntry *node;
  74.  
  75.             if (NULL == (node = malloc(sizeof(struct DirEntry))))
  76.                   return ERROR;
  77.             base->next = node;
  78.             strcpy(base->fname, ff.name);
  79.             node->next = NULL;
  80.             *node->fname = '\0';
  81.             base = node;
  82.             
  83.       } while (SUCCESS == _dos_findnext(&ff));
  84.       return SUCCESS;
  85. }
  86.  
  87. /*
  88. **  Simple directory lister
  89. */
  90.  
  91. void main(int argc, char *argv[])
  92. {
  93.       char *path;
  94.  
  95.       if (2 > argc)
  96.             path = ".";
  97.       else  path = argv[1];
  98.       if (ERROR == ReaDirArray(path))
  99.             printf("*** Could not read %s\n", path);
  100.       else
  101.       {
  102.             struct DirEntry *node = &DirRoot;
  103.             printf("Directory of %s\n\n", path);
  104.             while (node)
  105.             {
  106.                   puts(node->fname);
  107.                   node = node->next;
  108.             }
  109.       }
  110. }
  111.